const Stream = require("node-rtsp-stream");
const fs = require('fs');
const path = require('path');


// 设置rtsp视频流地址
const rtsp_urls1 = "rtsp://admin:zkhy@123@192.168.8.64:554/Streaming/Channels/101";
const rtsp_urls2 = "rtsp://admin:zkhy@123@192.168.8.64:554/Streaming/Channels/101";

function writeLog(){
  const count = fs.readFileSync(path.join(__dirname, './neterror.txt'), 'utf8');
  fs.writeFileSync('./neterror.txt',(Number(count) + 1).toString())
}
function initStream(url,port){
  let stream = new Stream({
    name: "sockets",
    streamUrl: url,
    wsPort: port,
    ffmpegOptions: {
      // 选项ffmpeg标志
      "-stats": "", // 没有必要值的选项使用空字符串
      "-r": 30 // 具有必需值的选项指定键后面的值<br>　　　　'-s':'1920*1080'
    }
  })
  stream.on("novideo", () => {
    console.log("novideonovideonovideonovideonovideo")
    stream.stop()
  })
  stream.on('exit', () => {
    console.log('exitexitexitexitexitexitexitexitexitexit222')
    stream.stop()
    writeLog()
  })
  stream.on('exitWithError', () => {
    stream.stop()
    writeLog()
  })
}

initStream(rtsp_urls1, 9998)
initStream(rtsp_urls2, 9999)
